//
//  YSY_Player_View.m
//  GoodManNews
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YSY_Player_View.h"

@implementation YSY_Player_View

- (void)dealloc
{
    [_player release];
    [_playerItem release];
    [_playerLayer release];
    [_playerLayerView release];
    [_controlToolView release];
    [_videoPlayProgress release];
    [_videoProgress release];
    [_videoVolumeSlider release];
    [_startOrStop_btn release];
    [_fullScreen_btn release];
    [_currentTime release];
    [_totalTime release];
    [_URlArr release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

+ (instancetype)shareData {
    static YSY_Player_View *hero = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hero = [[YSY_Player_View alloc] init];
    });
    return hero;
}
/**
 *  初始化播放器
 *
 *  @return 播放器对象
 */
-(AVPlayer *)player{
    if (!_player) {
        NSLog(@"倒萨倒萨大打算打算");
        NSURL *url = [NSURL URLWithString:self.URlArr[0]];
        self.playerItem = [AVPlayerItem playerItemWithURL:url];
        _player=[AVPlayer playerWithPlayerItem:self.playerItem];
        [self addobserverToPlayerItem];
        [self.player play];
    }
    return _player;
}

#pragma mark - URLArr setter方法

//- (AVPlayerItem *)createPlayerItem {
//    
//    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
//    
//    return playerItem;
//    
//}
//playerItem 添加监听
- (void)addobserverToPlayerItem {
    NSLog(@"添加监听");
    [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}

/**
 *  播放完成
 */
- (void)addNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidEnd) name:AVPlayerItemFailedToPlayToEndTimeNotification object:self.playerItem];
}
#pragma mark - 创建player 和 playerLayer

#pragma mark - 私有方法
- (void)playerDidEnd {
    NSLog(@"播放完成");
}
#pragma mark - KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    AVPlayerItem *playerItem = (AVPlayerItem *)object;
    if ([keyPath isEqualToString:@"status"] ) {
        if ([playerItem status] == AVPlayerItemStatusReadyToPlay) {
            NSLog(@"可以播放了");
            [self.player play];
        }else if ([playerItem status] == AVPlayerItemStatusFailed) {
            NSLog(@"请求失败");
        }
    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){
        
        
    }
    
}
#pragma mark - 视图
- (void)createView {
    
    self.playerLayerView = [UIView new];
    self.playerLayerView.backgroundColor  = [UIColor magentaColor];
    [self addSubview:self.playerLayerView];
    [self.playerLayerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.top.equalTo(self);
        make.height.mas_equalTo(HEIGHT / 3);

    }];
    NSLog(@"创建playerLayer");
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = CGRectMake(0, 0, WIDTH, HEIGHT  /3);
    self.playerLayer.backgroundColor = [[UIColor groupTableViewBackgroundColor] CGColor];
    [self.playerLayerView.layer addSublayer:self.playerLayer];
    
    
    
    
    self.controlToolView = [UIView new];
    self.controlToolView.backgroundColor  = [UIColor redColor];
    [self addSubview:self.controlToolView];
    [self.controlToolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self.playerLayerView.mas_bottom);
        make.height.mas_equalTo(HEIGHT / 12);
    }];
   
    
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 3);
    [self.playerLayerView.layer addSublayer:self.playerLayer];
    
    
    
    self.startOrStop_btn = [UIButton new];
    [self.startOrStop_btn setImage:[UIImage imageNamed:@"player_start"] forState:UIControlStateNormal];
    [self.startOrStop_btn setImage:[UIImage imageNamed:@"player_pause"] forState:UIControlStateSelected];
    self.startOrStop_btn.backgroundColor = [UIColor yellowColor];
    [self.controlToolView addSubview:self.startOrStop_btn];
    [self.startOrStop_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.controlToolView);
        make.top.equalTo(self.controlToolView);
        make.bottom.equalTo(self.controlToolView);
        make.width.mas_equalTo(WIDTH / 8);
    }];
    
    self.currentTime = [UILabel new];
    self.currentTime.textAlignment  = NSTextAlignmentCenter;
    self.currentTime.text = @"00:00";
    self.currentTime.backgroundColor = [UIColor greenColor];
    [self.controlToolView addSubview:self.currentTime];
    [self.currentTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.startOrStop_btn.mas_right);
        make.centerY.equalTo(self.controlToolView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(50, HEIGHT / 12));
    }];
    
    self.videoProgress = [UIProgressView new];
    self.videoProgress.backgroundColor  = [UIColor magentaColor];
    [self.controlToolView addSubview:self.videoProgress];
    [self.videoProgress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.currentTime.mas_right);
        make.centerY.equalTo(self.controlToolView.mas_centerY);
        make.width.mas_equalTo(WIDTH / 2);
        make.height.mas_equalTo(@2);
    }];
    
    self.videoPlayProgress = [UISlider new];
    [self.controlToolView addSubview:self.videoPlayProgress];
    [self.videoPlayProgress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.videoProgress);
    }];
    
    self.totalTime = [UILabel new];
    self.totalTime.textAlignment  = NSTextAlignmentCenter;
    self.totalTime.text = @"99:99";
    self.totalTime.backgroundColor = [UIColor greenColor];
    [self.controlToolView addSubview:self.totalTime];
    [self.totalTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.videoProgress.mas_right);
        make.centerY.equalTo(self.controlToolView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(50, HEIGHT / 12));

    }];
    
    self.fullScreen_btn = [UIButton new];
    [self.fullScreen_btn setImage:[UIImage imageNamed:@"player_quanping"] forState:UIControlStateNormal];
     [self.fullScreen_btn setImage:[UIImage imageNamed:@"player_quxiaoquanping"] forState:UIControlStateSelected];
    self.fullScreen_btn.backgroundColor = [UIColor yellowColor];
    [self.controlToolView addSubview:self.fullScreen_btn];
    [self.fullScreen_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.controlToolView);
        make.bottom.equalTo(self.controlToolView);
        make.left.equalTo(self.totalTime.mas_right);
        make.right.equalTo(self.controlToolView);

    }];
   
    
}
@end
